package com.lee.ssm.dao;

import com.lee.ssm.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IRoleDao {

    @Select("select * from Role where id in (select roleId from users_role where userId=#{userId})")
    @Results(id="role",
            value={
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "permissions", column = "id",javaType = java.util.List.class,many = @Many(select = "com.lee.ssm.dao.IPermissionDao.findPermissionByRoleId"))
            }
    )
    List<Role> findRoleByUserId(String userId)throws Exception;

    @Select("select * from role")
    List<Role> findAll()throws Exception;

    @Insert("insert into role(roleName,roleDesc) values(#{roleName},#{roleDesc})")
    void save(Role role)throws Exception;

    @ResultMap("role")
    @Select("select * from Role where id=#{id}")
    Role findById(String id)throws Exception;

    @Delete("delete from role where id=#{id}")
    void deleteRole(String id)throws Exception;
    @Delete("delete from users_role where roleId=#{id}")
    void deleteRoleToAllUser(String id) throws Exception;
    @Delete("delete from role_permission where roleId=#{id}")
    void deleteRoleToAllPermission(String id) throws Exception;

     @Select("select * from role where id not in (select roleId from users_role where userId=#{id})")
    List<Role> findOthersRoleByUserId(String id)throws Exception;
    @Insert("insert into role_permission values (#{permissionId},#{roleId})")
    void addPermissionToRole(@Param("roleId") String roleId,@Param("permissionId") String permissionId);
}
